package com.adam.masterSpringMVC4.weibo;

import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Repository
public class WeiboUserRepository {

    private final Map<String, WeiboUser> userMap = new ConcurrentHashMap<>();

    public WeiboUser save(String email, WeiboUser user) {
        user.setEmail(email);
        return userMap.put(email, user);
    }

    public WeiboUser save(WeiboUser user) {
        return save(user.getEmail(), user);
    }

    public WeiboUser findOne(String email) {
        return userMap.get(email);
    }

    public List<WeiboUser> findAll() {
        return new ArrayList<>(userMap.values());
    }

    public void delete(String email) {
        userMap.remove(email);
    }

    public boolean exists(String email) {
        return userMap.containsKey(email);
    }
}
